/*

 * Licensed to the Apache Software Foundation (ASF) under one

 * or more contributor license agreements.  See the NOTICE file

 * distributed with this work for additional information

 * regarding copyright ownership.  The ASF licenses this file

 * to you under the Apache License, Version 2.0 (the

 * "License"); you may not use this file except in compliance

 * with the License.  You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

package com.bff.gaia.unified.runners.fnexecution.environment;



import com.bff.gaia.unified.model.pipeline.v1.RunnerApi.Environment;

import com.bff.gaia.unified.runners.fnexecution.GrpcFnServer;

import com.bff.gaia.unified.runners.fnexecution.artifact.ArtifactRetrievalService;

import com.bff.gaia.unified.runners.fnexecution.control.ControlClientPool;

import com.bff.gaia.unified.runners.fnexecution.control.FnApiControlClientPoolService;

import com.bff.gaia.unified.runners.fnexecution.control.InstructionRequestHandler;

import com.bff.gaia.unified.runners.fnexecution.logging.GrpcLoggingService;

import com.bff.gaia.unified.runners.fnexecution.provisioning.StaticGrpcProvisionService;

import com.bff.gaia.unified.sdk.fn.IdGenerator;



/**

 * An {@link EnvironmentFactory} that creates StaticRemoteEnvironment used by a runner harness that

 * would like to use an existing InstructionRequestHandler.

 */

public class StaticRemoteEnvironmentFactory implements EnvironmentFactory {

  public static StaticRemoteEnvironmentFactory forService(

      InstructionRequestHandler instructionRequestHandler) {

    return new StaticRemoteEnvironmentFactory(instructionRequestHandler);

  }



  private final InstructionRequestHandler instructionRequestHandler;



  private StaticRemoteEnvironmentFactory(InstructionRequestHandler instructionRequestHandler) {

    this.instructionRequestHandler = instructionRequestHandler;

  }



  @Override

  public RemoteEnvironment createEnvironment(Environment environment) {

    return StaticRemoteEnvironment.create(environment, this.instructionRequestHandler);

  }



  /** Provider for StaticRemoteEnvironmentFactory. */

  public static class Provider implements EnvironmentFactory.Provider {

    private final InstructionRequestHandler instructionRequestHandler;



    public Provider(InstructionRequestHandler instructionRequestHandler) {

      this.instructionRequestHandler = instructionRequestHandler;

    }



    @Override

    public EnvironmentFactory createEnvironmentFactory(

        GrpcFnServer<FnApiControlClientPoolService> controlServiceServer,

        GrpcFnServer<GrpcLoggingService> loggingServiceServer,

        GrpcFnServer<ArtifactRetrievalService> retrievalServiceServer,

        GrpcFnServer<StaticGrpcProvisionService> provisioningServiceServer,

        ControlClientPool clientPool,

        IdGenerator idGenerator) {

      return StaticRemoteEnvironmentFactory.forService(this.instructionRequestHandler);

    }

  }

}